package cn.edu.cqu.bysj.services.hibernate;

import java.util.List;

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate4.HibernateTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.edu.cqu.bysj.domains.StopStationInfo;
import cn.edu.cqu.bysj.services.StopStationInfoCRUD;


@Service("StopStationInfoCRUDHibernate")
public class StopStationInfoCRUDHibernate implements StopStationInfoCRUD {

	private HibernateTemplate ht;
	@Autowired
	public void setSessionFactory(SessionFactory sessionFactory) {
		ht = new HibernateTemplate(sessionFactory);
	}
	
	@Override
	@Transactional
	public void add(StopStationInfo staticStationInfo) {
		ht.save(staticStationInfo);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<StopStationInfo> getByName(String name) {
		String sql = "from StopStationInfo t where t.stationName=?";
		List<StopStationInfo> ssis = (List<StopStationInfo>) ht.find(sql, new Object[]{name});
		if (ssis.size() == 0)
			return null;
		return ssis;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<StopStationInfo> getAll() {
		List<StopStationInfo> infos = (List<StopStationInfo>) ht.find("from StopStationInfo");
		return infos;
	}

	@Transactional
	@Override
	public void edit(int stopStationInfoNo,StopStationInfo stopStationInfo) {
		StopStationInfo ssi = this.getByNo(stopStationInfoNo);
		ssi.setArriveTime(stopStationInfo.getArriveTime());
		ssi.setStartTime(stopStationInfo.getStartTime());
		ssi.setStopoverTime(stopStationInfo.getStopoverTime());
		ssi.setStationName(stopStationInfo.getStationName());
		ssi.setStationNo(stopStationInfo.getStationNo());
		ssi.setTrainNo(stopStationInfo.getTrainNo());
		ssi.setDayDiff(stopStationInfo.getDayDiff());
		ht.update(ssi);
	}

	@Transactional
	@Override
	public void deleteByName(String name) {
		List<StopStationInfo> infos = this.getByName(name);
		for (StopStationInfo stopStationInfo : infos) {
			ht.delete(stopStationInfo);
		}
	}
	
	@Override
	@Transactional
	public void deleteAll()
	{
		List<StopStationInfo> infos = this.getAll();
		for (StopStationInfo stopStationInfo : infos) {
			this.deleteByName(stopStationInfo.getStationName());
		}
	}

	@Override
	public StopStationInfo getByNo(int stopStationInfoNo) {
		return ht.get(StopStationInfo.class, stopStationInfoNo);
	}

	@Override
	@Transactional
	public void deleteByNo(int stopStationInfoNo) {
		StopStationInfo stopStationInfo = this.getByNo(stopStationInfoNo);
		ht.delete(stopStationInfo);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<StopStationInfo> getByTrainNo(String trainNo)
	{
		String sql = "from StopStationInfo t where t.trainNo=?";
		List<StopStationInfo> ssis = (List<StopStationInfo>) ht.find(sql, new Object[]{trainNo});
		if (ssis.size() == 0)
			return null;
		return ssis;
	}

	@SuppressWarnings("unchecked")
	@Override
	public StopStationInfo getStopStationInfo(String trainNo, String stationName)
	{
		String sql = "from StopStationInfo t where t.trainNo=? and t.stationName=?";
		List<StopStationInfo> ssis = (List<StopStationInfo>) ht.find(sql, new Object[]{trainNo,stationName});
		if (ssis.size() == 0)
			return null;
		return ssis.get(0);
	}

}
